package com.guagua.chat.topic;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.json.JSONUtils;

import com.guagua.chat.base.BaseAction;
import com.opensymphony.webwork.ServletActionContext;
/**
 * 房间首页-说说
 * @author liyi
 * @version 2012-7-2 9:00:00
 */
public class TopicAction extends BaseAction{
	
	private String roomId;//房间ID
	
	private TopicManager _tManager = TopicManager.getInstance();
	private long userId;
	
	private String roomMaster;//是否是房主或超管
	
	public String getRoomId() {
		return roomId;
	}
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	
	
	
	public TopicManager get_tManager() {
		return _tManager;
	}
	public void set_tManager(TopicManager _tManager) {
		this._tManager = _tManager;
	}
	public String getRoomMaster() {
		return roomMaster;
	}
	public void setRoomMaster(String roomMaster) {
		this.roomMaster = roomMaster;
	}
	
	/**
	 * 新发布说说主题
	 * @return 如果成功 跳转
	 * @throws Exception 
	 */
	public String createTopic() throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		String content = request.getParameter("content");
		String isSyn2MySpaceChk = request.getParameter("isSyn2MySpaceChk");
		
		
		
		
		//同步发布空间
		if(isSyn2MySpaceChk!=null){
			new SynchroToZone(this.getUserId(), content, this.getRequest()).start();
		}
		//封装对象
		TopicInfo _tTopicInfo = new TopicInfo();
		_tTopicInfo.setContent(content);
		_tTopicInfo.setOp_time(System.currentTimeMillis());
		_tTopicInfo.setCreate_time(System.currentTimeMillis());
		_tTopicInfo.setRoom_id(Long.parseLong(roomId));
		_tTopicInfo.setUser_id(this.getUserId());
		_tTopicInfo.setUser_nick(this.getAuthToken().getNickname());
		boolean val = _tManager.createTopicOrComment(_tTopicInfo, roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 查找所有SHUOSHUO 主体并分页显示
	 * @return
	 * @throws Exception
	 */
	public String findTopicAll()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String pageNoStr = request.getParameter("pageNo");
		int pageNo = 1;
		if(pageNoStr!=null && !pageNoStr.equals("")){
			pageNo = Integer.parseInt(pageNoStr);
		}
		Map<String,Object> map = _tManager.findTopicAllByRoomId(roomId, pageNo);
		map.put("pageNo", pageNo);
		String jsonString = JSONObject.fromObject(map).toString(); 
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		System.out.println("jsonString = "+jsonString);
		response.getWriter().write(jsonString);
		return null;
	}
	/**
	 * 删除说说主题
	 * @return
	 * @throws Exception
	 */
	public String delTopicById()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long id = Long.parseLong(request.getParameter("id"));//topicId
		boolean val = _tManager.delTopicById(roomId, id);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 加载评论
	 * @return
	 * @throws Exception
	 */
	public String findCommentByTopicId()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String pageNoStr = request.getParameter("pageNo");
		long topicId = Long.parseLong(request.getParameter("id"));//topicId
		int pageNo = 1;
		if(pageNoStr!=null && !pageNoStr.equals("")){
			pageNo = Integer.parseInt(pageNoStr);
		}
		Map<String,Object> map = _tManager.findTopicCommentsByRoomId(roomId, topicId,pageNo);
		map.put("pageNo", pageNo);
		map.put("topicId", topicId);
		String jsonString = JSONObject.fromObject(map).toString(); 
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		System.out.println("jsonString = "+jsonString);
		response.getWriter().write(jsonString);
		return null;
	}
	
	/**
	 * 新发布说说主题
	 * @return 如果成功 跳转
	 * @throws Exception 
	 */
	public String createComment() throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long parent_id = Long.parseLong(request.getParameter("parentId"));
		String content = request.getParameter("content");
		//封装对象
		TopicInfo _tTopicInfo = new TopicInfo();
		_tTopicInfo.setContent(content);
		_tTopicInfo.setOp_time(System.currentTimeMillis());
		_tTopicInfo.setCreate_time(System.currentTimeMillis());
		_tTopicInfo.setRoom_id(Long.parseLong(roomId));
		_tTopicInfo.setUser_id(getAuthToken().getUserID());
		_tTopicInfo.setUser_nick(getAuthToken().getNickname());
		_tTopicInfo.setParent_id(parent_id);
		boolean val = _tManager.createTopicOrComment(_tTopicInfo, roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	/**
	 * 删除评论
	 * @return
	 * @throws Exception
	 */
	public String delCommentById()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		long id = Long.parseLong(request.getParameter("id"));//topicId
		long parentId = Long.parseLong(request.getParameter("parentId"));//parentId
		boolean val = _tManager.delCommentById(id,parentId,roomId);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", val);
		response.setHeader("Content-type", "application/json");
		response.setHeader("Cache-Control","no-cache");
		response.setHeader("expires", "-1");
		response.getWriter().write(jsonObject.toString());
		return null;
	}
	
	/**
	 * 返回帖子说说主页面
	 * @return
	 * @throws Exception
	 */
	public String index()throws Exception{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		if("1".equals(roomMaster)){//是房主直接返回
			return "TOPIC";
		}
//		roomId = roomId.equals("")?"0":roomId;
		int isSuperAdmin = _tManager.isSuperAdmin(this.getUserId(),this.roomId);
		if(isSuperAdmin == 1){
			roomMaster = "1";//是超级管理员
		}
		return "TOPIC";
		
		
	}

}
